<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category StockArticleService.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/4/17  17:00
 * +--------------------------------------------------------+
 */

namespace App\Services;

use App\Jobs\YbsArticleJob;
use App\Repositories\StockArticleRepository;
use App\Util\FileSystem;
use Carbon\Carbon;

class StockArticleService extends BaseService
{
    protected $stockArticleRepository;

    private $ybs_url = 'https://admin.gbhome.com/api/v4/common/3in1/discovery?pageNum=1&pageSize=10';

    public function __construct(StockArticleRepository $stockArticleRepository)
    {
        $this->stockArticleRepository = $stockArticleRepository;
    }

    /**
     * @param $params
     * @return array
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/18  14:17
     */
    public function getList($params)
    {
        $list = $this->stockArticleRepository->getList($params);

        return $this->initPaginateData($list);
    }

    public function getInfo($params)
    {
        $array_no_image = [
            '短线解盘',
        ];

        $data = $this->stockArticleRepository->getInfo($params);

        $res          = [];
        $res['title'] = $data->title;
        if (!empty($data->has_one_article_stock_content)) {
            $res['details'] = $data->has_one_article_stock_content->detail;
        } elseif (!empty($data->content)) {
            $res['details'] = $data->content;
        } elseif (!empty($data->summary)) {
            $res['details'] = $data->summary;
        }
        $res['show_url'] = '';
        if (!in_array($data['zlName'], $array_no_image) || empty($data['content'])) {
            $show_url = 'https://yanbaoshe.oss-cn-beijing.aliyuncs.com/images/prod-gb-share_zlArticle_' . $data['subjectId'] . '.png';
            // 拉取图片到本地
            $fileSystem = $fileSystem = new FileSystem('');
            $res['show_url'] = $fileSystem->getImage($show_url, 'prod-gb-share_zlArticle_' . $data['subjectId'] . '.png');
        }

        return $res;

    }

    public function addData($params)
    {
        /**
         * es 只用作学习，服务器太小无法支撑
         */
        // $esClient = new ElasticSearchService('ybs_log');
        //
        // foreach ($params as $val) {
        //     $res = $esClient->putDoc($val);
        // }

        return $add = $this->stockArticleRepository->addData($params);
    }

    public function collectYbsArticle()
    {
        $now    = Carbon::now();
        $hours  = $now->hour;
        $minute = $now->minute;
        if (($hours < 8) || $hours > 15) {
            if ($minute % 30 != 0) {
                return true;
            }
        }

        try {
            $data = curl($this->ybs_url);
        } catch (\Exception $e) {
            return false;
        }

        if (empty($data)) {
            return true;
        }
        $list = json_decode($data, true);

        $insert_data = [];

        $db_data_select = $this->stockArticleRepository->getListByLimit();

        foreach ($list['data']['records'] as $value) {
            //$db_data_select 上面取出了20条，这边不经过数据库，减小数据库压力
            $db_data = $db_data_select->where('subjectId', $value['subjectId'])->where('type', $value['type'])->first();

            if ($db_data) {
                continue;
            }

            $collect_data                    = [];
            $collect_data['title']           = $value['title'];
            $collect_data['created_at']      = $value['createTime'];
            $collect_data['updated_at']      = $value['updateTime'];
            $collect_data['type']            = $value['type'];
            $collect_data['content']         = $value['content'];
            $collect_data['subjectId']       = $value['subjectId'];
            $collect_data['summary']         = $value['summary'];
            $collect_data['readingSum']      = $value['readingSum'];
            $collect_data['commentSum']      = $value['commentSum'];
            $collect_data['cover']           = $value['cover'];
            $collect_data['upTime']          = $value['upTime'];
            $collect_data['isOpen']          = $value['isOpen'];
            $collect_data['openStick']       = $value['openStick'];
            $collect_data['zlAvatarUrl']     = json_encode(array_merge([$value['zlAvatarUrl']], $value['imgList']));
            $collect_data['zlName']          = $value['zlName'];
            $collect_data['zlSubscribeType'] = $value['zlSubscribeType'];
            $collect_data['zlType']          = $value['zlType'];

            $insert_data[] = $collect_data;
        }

        if (!empty($insert_data)) {
            // dispatch((new YbsArticleJob($insert_data))->onQueue('ybs_article'));
            dispatch((new YbsArticleJob($insert_data))->onQueue('default'));
        }
        //获取文章内容
        return true;

    }

}
